package com.example.ecas.persistence;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.ecas.pojo.Board;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Repository
@Mapper
public interface BoardMapper extends BaseMapper<Board> {

    // 返回指定id的公告
    default Board getBoard(int id) {
        return this.selectById(id);
    }

    // 新增公告
    default int addBoard(Board board) {
        if (board == null) {
            return 0;
        }
        QueryWrapper<Board> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", board.getId());
        if (!this.exists(queryWrapper)) {
            return this.insert(board);
        }
        return 0;
    }

    // 修改公告,board必须已经在数据库中存在
    default int updateBoard(Board board) {
        if (board != null) {
            return this.updateById(board);
        }
        return 0;
    }

    // 删除指定id的公告
    default int deleteBoard(int id) {
        return this.deleteById(id);
    }

}
